#include "SkinImageGdiplus.h"
#include <Shlwapi.h>
#pragma comment(lib,"Shlwapi.lib")
#pragma comment(lib,"Gdiplus.lib")

ULONG_PTR CSkinImageGdiplus::m_gdiplusToken = 0;
GdiplusStartupInput CSkinImageGdiplus::m_gdiplusStartupInput;
CString CSkinImageGdiplus::m_skinFolder;

CSkinImageGdiplus::CSkinImageGdiplus( void )
{
    m_pGdiplusImg = NULL;
}


CSkinImageGdiplus::~CSkinImageGdiplus( void )
{
    UnLoad();
}

void CSkinImageGdiplus::InitGdiplus()
{
    GdiplusStartup( &m_gdiplusToken, &m_gdiplusStartupInput, NULL );
}

void CSkinImageGdiplus::UnInitGdiplus()
{
    GdiplusShutdown( m_gdiplusToken );
}

BOOL CSkinImageGdiplus::Load( LPCTSTR lpszFilePath )
{
    if ( !PathFileExists( lpszFilePath ) )
    {
        return FALSE;
    }

    m_pGdiplusImg = new Image( lpszFilePath );
    return IsLoaded();
}

BOOL CSkinImageGdiplus::IsLoaded()
{
    return ( NULL != m_pGdiplusImg && ( Ok == m_pGdiplusImg->GetLastStatus() ) );
}

void CSkinImageGdiplus::UnLoad()
{
    if ( NULL != m_pGdiplusImg )
    {
        delete m_pGdiplusImg;
        m_pGdiplusImg = NULL;
    }
}

void CSkinImageGdiplus::Draw( HDC hDC, const RECT& rc )
{
    if ( !IsLoaded() ) return ;

    Graphics g( hDC );
    g.DrawImage( m_pGdiplusImg, Gdiplus::Rect( rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top ) );
}

void CSkinImageGdiplus::Draw( HDC hDC, const POINT& pt )
{
    if ( !IsLoaded() )
    {
        return ;
    }

    Graphics g( hDC );
    Gdiplus::Point gdiPt;
    gdiPt.X = pt.x;
    gdiPt.Y = pt.y;
    g.DrawImage( m_pGdiplusImg, gdiPt );
}

void CSkinImageGdiplus::Draw( HDC hDC, int iDestX, int iDestY )
{
    POINT pt;
    pt.x = iDestX;
    pt.y = iDestY;
    Draw( hDC, pt );
}

void CSkinImageGdiplus::OnPaint( HDC hDC )
{
    Draw( hDC );
}

void CSkinImageGdiplus::NodeLoadComplete()
{
    Load( m_skinFolder + m_imageSrc );
}

void CSkinImageGdiplus::SetSkinFolder( LPCTSTR skinFolder )
{
    m_skinFolder = skinFolder;
}
